package hn.cch.spring_async_call.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;

/**
 * 使用springboot发送邮件
 */
@Component
public class MailUtil {

    private static Logger logger = LoggerFactory.getLogger(MailUtil.class);


    // static {
    //     System.setProperty("mail.mime.splitlongparameters", "false");
    //     System.setProperty("mail.mime.charset", "UTF-8");
    // }

    // @Resource
    // private JavaMailSender javaMailSender;

    private JavaMailSenderImpl javaMailSender;

    // @PostConstruct
    public void init(){
        javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.163.com");
        javaMailSender.setPort(25);
        javaMailSender.setUsername("hangcchhn@163.com");
        javaMailSender.setPassword("******");
        javaMailSender.setProtocol("smtp");
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        javaMailSender.setJavaMailProperties(properties);
    }

    public void sendTextMail(String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("hangcchhn@163.com");
        message.setTo("hangcchhn@qq.com");
        message.setSubject("标题");
        message.setText(text);
        javaMailSender.send(message);
    }

    public void sendHtmlMail(String html) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom("hangcchhn@163.com");
            mimeMessageHelper.setTo("hangcchhn@qq.com");
            mimeMessageHelper.setSubject("标题");
            mimeMessageHelper.setText(html, true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            logger.error("send file mail error:{}", e.getMessage());
        }
    }

    public void sendFileMail(File file) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom("hangcchhn@163.com");
            mimeMessageHelper.setTo("hangcchhn@qq.com");
            mimeMessageHelper.setSubject("标题");
            mimeMessageHelper.setText("内容");
            if (file != null) {
                FileSystemResource fileSystemResource = new FileSystemResource(file);
                mimeMessageHelper.addAttachment(file.getName(), fileSystemResource);
            }
            javaMailSender.send(mimeMessage);

        } catch (MessagingException e) {
            logger.error("send file mail error:{}", e.getMessage());
        }
    }





}
